dofile (getScriptPath() .. "\\..\\processor\\processor.lua")

if not window then

window = {t_id = {}, caption = {}, cell = {}}


processor:AddStop(function()
	for i, v in ipairs(window.t_id) do
		DestroyTable(v)
	end
	
	window.t_id = {}
	window.caption = {}
	window.cell = {}
end)


function window:new()
	local t_id = AllocTable()
	
	if t_id ~= nil then
		table.sinsert(self.t_id, t_id)
		
		return #self.t_id
	end
end


function window:id(nwin)
	return self.t_id[nwin or 1]
end


function window:size(nwin)
	return GetTableSize(self:id(nwin))
end


function window:AddColumn(width, nwin)
	local rows, cols = self:size(nwin)
	
	return AddColumn(self:id(nwin), cols + 1, "", true, QTABLE_STRING_TYPE, width)
end


function window:AddRow(nwin)
	if window:IsClosed(nwin) then
		CreateWindow(self:id(nwin))
	end
	
	return InsertRow(self:id(nwin), -1)
end


function window:show(nwin)
	if window:IsClosed(nwin) then
		CreateWindow(self:id(nwin))
	end
end


function window:IsClosed(nwin)
	return IsWindowClosed(self:id(nwin))
end


function window:GetCaption(nwin)
	if not self.caption[nwin or 1] then
		self.caption[nwin or 1] = GetWindowCaption(self:id(nwin))
	end
	
	return self.caption[nwin or 1]
end


function window:SetCaption(text, nwin)
	text = tostring(text)
	
	if self.caption[nwin or 1] ~= text then
		self.caption[nwin or 1] = text
		SetWindowCaption(self:id(nwin), self.caption[nwin or 1])
	end
end


function window:SetCell(row, col, text, nwin)
	text = tostring(text)
	
	if not self.cell[nwin or 1] then self.cell[nwin or 1] = {} end
	if not self.cell[nwin or 1][row] then self.cell[nwin or 1][row] = {} end
	
	if self.cell[nwin or 1][row][col] ~= text then
		self.cell[nwin or 1][row][col] = text
		SetCell(self:id(nwin), row, col, text)
	end
end


function window:SetColor(row, col, color, nwin)
	if type(color) ~= "table" then
		color = {b_color = color}
	end
	
	SetColor(self:id(nwin), row, col,
		color.b_color or QTABLE_DEFAULT_COLOR,
		color.f_color or QTABLE_DEFAULT_COLOR,
		color.sel_b_color or color.b_color or QTABLE_DEFAULT_COLOR,
		color.sel_f_color or color.f_color or QTABLE_DEFAULT_COLOR
	)
end


end -- of file
